package com.bidcms.util;

public class PageHtmlStrUtil {

	private long total;//总数
	private int pageNo;//当前页数
	private int pageSize;//每页数量
	private int totalPages;//总页数
	private boolean hasNextPage = false; // 是否有下一页
	private boolean hasPreviousPage = false; // 是否有前一页

	public PageHtmlStrUtil(long total, int pageNo, int pageSize) {
		int Remainder = (int) (total%pageSize);//余数
		totalPages = (int) (total/pageSize);
		totalPages+=(Remainder>0?1:0);
		this.total = total;
		this.pageNo = pageNo;
		this.pageSize = pageSize;
		hasPreviousPage = false;
        if (pageNo >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
	}
	/**
     * 处理分页
     */
    public void disposePage() {
        if (pageNo == 0) {
            pageNo = 1;
        }
        if ((pageNo - 1) > 0) {
            hasPreviousPage = true;
        } else {
            hasPreviousPage = false;
        }

        if (pageNo >= totalPages) {
            hasNextPage = false;
        } else {
            hasNextPage = true;
        }
    }
	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String displayForPage(String method) {
		StringBuffer sb = new StringBuffer();
		sb.append("<div class=\"w-page\"><label class=\"fr\">");

		// 判断是否有上一页
		if (this.isHasPreviousPage()) {
			sb.append("<a href=\"javascript:void(0)\" onclick=\"" + method
					+ "('" + String.valueOf(this.getPageNo() - 1)
					+ "');return false;\">上一页</a>");
		} else {
			sb.append("<span class=\"disabled\">上一页</span>");
		}

		// 中间显示
		for (int i = 1; i <= this.getTotalPages(); i++) {
			String spanClzz = "<a href=\"javascript:void(0)\" onclick=\""
					+ method + "('" + String.valueOf(i) + "');return false;\">"
					+ i + "</a>";

			if (this.pageNo == i) {
				spanClzz = "<span class='current'>" + i + "</span>";
			}
			sb.append(spanClzz);

			// 当大于9页数的时候才进行分页显示
			if (this.getTotalPages() - 2 > 7) {
				if (i == 5) {
					i = this.getTotalPages() - 2;
					sb.append("...");
				}
			}
		}
		// 判断是否有下一页
		if (this.isHasNextPage()) {
			sb.append("<a href=\"javascript:void(0)\" onclick=\"" + method
					+ "('" + String.valueOf(this.getPageNo() + 1)
					+ "');return false;\">下一页</a>");

		} else {
			sb.append("<span class=\"disabled\">下一页</span>");
		}

		sb.append("</div>");
		return sb.toString();
	}

	public String displayForPage() {
		//.pagenavi .total,.pagenavi .prev,.pagenavi .next,.pagenavi .num,.pagenavi .goto-input,.pagenavi .goto-btn
		//.pagenavi .prev,.pagenavi .next,.pagenavi .num,.pagenavi .goto
		StringBuffer sb = new StringBuffer();
		//<a class="num" href="#">1</a>

		//<a class="active">2</a>

		//<a class="num" href="#">3</a>

		// 判断是否有上一页
/*		if (this.isHasPreviousPage()) {
			sb.append("<a href=\"javascript:void(0)\" class=\"fetch_page\">上一页</a>");
		} else {
			sb.append("<span >上一页</span>");
		}*/
		sb.append("<span class=\"total\">共 "+total+" 条，每页 20 条</span>");
		// 中间显示
		for (int i = 1; i <= this.getTotalPages(); i++) {
			String spanClzz = "<a href=\"javascript:void(0)\" class=\"fetch_page num\" data-page-num=\""+i+"\">" + i + "</a>";

			if (this.pageNo == i) {
				spanClzz = "<span class=\"active\">" + i + "</span>";
			}
			sb.append(spanClzz);

			// 当大于9页数的时候才进行分页显示
			if (this.getTotalPages() - 2 > 7) {
				if (i == 5) {
					i = this.getTotalPages() - 2;
					sb.append("...");
				}
			}
		}
		// 判断是否有下一页
/*		if (this.isHasNextPage()) {
			sb.append("<a href=\"javascript:void(0)\" onclick=\"findDistResult('"
					+ String.valueOf(this.getPageNo() + 1)
					+ "');return false;\">下一页</a>");
		} else {
			sb.append("<span class=\"disabled\">下一页</span>");
		}*/
		sb.append("<div><input type=\"text\" class=\"js-goto-input goto-input\"/></div>");
		return sb.toString();
	}

	// 判断要不要分页
	public boolean isNext() {
		return total > 5;
	}

	public boolean isHasNextPage() {
		return hasNextPage;
	}

	public boolean isHasPreviousPage() {
		return hasPreviousPage;
	}

	public void setHasPreviousPage(boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
}
